package com.xone.xapp;

import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.xone.refresh.library.PullToRefreshBase;
import com.xone.refresh.library.PullToRefreshBase.Mode;
import com.xone.refresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.xone.refresh.library.PullToRefreshBase.OnRefreshListener;
import com.xone.refresh.library.PullToRefreshListView;

public final class PullToRefreshListActivity extends Activity {

	//MENU itemid 
	/**手动执行刷新*/
	static final int MENU_MANUAL_REFRESH = 0;
	/** 数据加载中是否允许滑动*/
	static final int MENU_DISABLE_SCROLL = 1;
	/**刷新模式，
	 * 包括{
	 * 1.DISABLED(0x0)  			禁止通过手势和手动执行
	 * 2.PULL_FROM_START(0x1)		可执行下拉刷新
	 * 3.PULL_FROM_END(0x2)			可执行上拉刷新
	 * 3.BOTH(0x3)					上下都可执行
	 * 4.MANUAL_REFRESH_ONLY(0x4)	禁止通过手势执行，但可以手动设置
	 * }*/
	static final int MENU_SET_MODE = 2;
	//MENU itemid end
	/**这里我们来判断是下拉还是上拉*/
	private Mode CurrentMode;
	
	private LinkedList<String> mListItems;
	
	private PullToRefreshListView mPullRefreshListView;
	
	private ArrayAdapter<String> mAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pull_refresh_demo_list);

		mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
		mPullRefreshListView.setMode(Mode.BOTH);

		//监听列表被刷新时事件.
		mPullRefreshListView
				.setOnRefreshListener(new OnRefreshListener<ListView>() {
					@Override
					public void onRefresh(
							PullToRefreshBase<ListView> refreshView) {
						
						int flags=DateUtils.FORMAT_SHOW_TIME
								| DateUtils.FORMAT_SHOW_DATE
								| DateUtils.FORMAT_ABBREV_ALL;
						
						String label = DateUtils.formatDateTime(
								getApplicationContext(),
								System.currentTimeMillis(),flags
								);
						CurrentMode=refreshView.getCurrentMode();
						// 更新最后刷新时间
						refreshView.getLoadingLayoutProxy()
								.setLastUpdatedLabel(label);

						// 执行加载更多数据任务.
						new GetDataTask().execute();
					}
				});

		//监听滑动到底部的事件
		mPullRefreshListView
				.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {

					@Override
					public void onLastItemVisible() {
						Toast.makeText(PullToRefreshListActivity.this,
								"已到列表底部!", Toast.LENGTH_SHORT).show();
					}
				});

		ListView actualListView = mPullRefreshListView.getRefreshableView();

		mListItems = new LinkedList<String>();
		mListItems.addAll(Arrays.asList(mStrings));

		mAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, mListItems);
		actualListView.setAdapter(mAdapter);
	}

	private class GetDataTask extends AsyncTask<Void, Void, String[]> {

		@Override
		protected String[] doInBackground(Void... params) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
			}
			return mStrings;
		}

		@Override
		protected void onPostExecute(String[] result) {
			//这里是提供给我们比较MODE的方法，返回0则表示相当
			if (CurrentMode.compareTo(Mode.PULL_FROM_START)==0) {
				mListItems.addFirst("+下拉新增数据" + new Date().getTime());
			}else {
				mListItems.addLast("+上拉新增数据" + new Date().getTime());
			}
			
			mAdapter.notifyDataSetChanged();

			//当数据加载完成，需要调用onRefreshComplete.
			mPullRefreshListView.onRefreshComplete();

			super.onPostExecute(result);
		}
	}
	/**
	 * 此方法只在初次加载时调用
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, MENU_MANUAL_REFRESH, 0, "手动刷新");
		menu.add(
				0,
				MENU_DISABLE_SCROLL,
				1,
				mPullRefreshListView.isScrollingWhileRefreshingEnabled() ? "加载中禁止滑动"
						: "加载中可滑动");
		menu.add(
				0,
				MENU_SET_MODE,
				0,
				mPullRefreshListView.getMode() == Mode.BOTH ? "模式：下拉刷新"
						: "模式：上下拉刷新");
		return super.onCreateOptionsMenu(menu);
	}
	/**
	 * 此方法每次点击都会调用
	 */
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		MenuItem disableItem = menu.findItem(MENU_DISABLE_SCROLL);
		disableItem
				.setTitle(mPullRefreshListView
						.isScrollingWhileRefreshingEnabled() ? "加载中禁止滑动"
						: "加载中可滑动");

		MenuItem setModeItem = menu.findItem(MENU_SET_MODE);
		setModeItem
				.setTitle(mPullRefreshListView.getMode() == Mode.BOTH ? "模式：下拉刷新"
						: "模式：上下拉刷新");

		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case MENU_MANUAL_REFRESH:
			new GetDataTask().execute();
			mPullRefreshListView.setRefreshing(false);
			break;
		case MENU_DISABLE_SCROLL:
			mPullRefreshListView
					.setScrollingWhileRefreshingEnabled(!mPullRefreshListView
							.isScrollingWhileRefreshingEnabled());
			break;
		case MENU_SET_MODE:
			mPullRefreshListView
					.setMode(mPullRefreshListView.getMode() == Mode.BOTH ? Mode.PULL_FROM_START
							: Mode.BOTH);
			break;
		}

		return super.onOptionsItemSelected(item);
	}

	private String[] mStrings = { 	
//			"当前数据", "当前数据", "当前数据", "当前数据", "当前数据",
//									"当前数据", "当前数据", "当前数据", "当前数据", "当前数据", 
//									"当前数据", "当前数据", "当前数据", "当前数据", "当前数据", 
//									"当前数据", "当前数据", "当前数据", 
									"当前数据", "当前数据" };
}
